package com.example.milktea.service.impl;

import com.example.milktea.dao.ProductDao;
import com.example.milktea.entity.Product;
import com.example.milktea.service.ProductService;
import com.example.milktea.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class ProductServiceImpl implements ProductService{
    @Autowired
    private ProductDao productDao;

    @Autowired
    private StoreService storeService;

    public List<Map<String, Object>> getProAll(){
        return  productDao.getProAll();
    };
    public Product getProductById(Integer id){
        return productDao.getProductById(id);
    }
    public void modifyPrice(String product_id, Integer newPrice) {
        this.productDao.modifyPrice(product_id,newPrice);
    }

//    //
//    public void onoffSale(String product_id) {
//        Product product = getProductById(Integer.valueOf(product_id));
//
//        String material[] = product.getProduct_store().split("-");
//        int length = material.length;
//        for(int i = 0;i < length; i++,length--){
//            if(storeService.query(material[i]) <=0){
//                product.setProduct_status(0);
//                productDao.updateStatus(product_id,0);
//                break;
//            }
//        }
//        if(length == 0){
//            product.setProduct_status(1);
//            productDao.updateStatus(product_id,1);
//        }
//    }
    public void onSale(String product_id){
        productDao.updateStatus(product_id,1);
    }

    public void offSale(String product_id){
        productDao.updateStatus(product_id,0);
    }

    public void updateProfit(String product_id) {
        Product product = getProductById(Integer.valueOf(product_id));
        String material[] = product.getProduct_store().split("-");
        int length = material.length;
        int newProfit =0;
        for(int i = 0;i < length; i++){
            newProfit+=this.productDao.queryCost(Integer.valueOf(material[i]));
        }
        this.productDao.updateProfit(product_id,newProfit);
    }

    public void addProduct(Product product){
         this.productDao.addProduct(product);
    }
}
